<?php

namespace app\admin\model\agent;

use think\Db;
use think\Model;


class Valid extends Model
{





    // 表名
    protected $name = 'valid_user';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    /**
     * 更新有效用户
     */
    public static function  userUpdate($orderInfo)
    {
        $info = self::where('uid',$orderInfo['uid'])->find();
        if(!empty($info)){
            return true;
        }
        $data = array(
            "uid"=>$orderInfo['uid'],
            "orderId"=>$orderInfo['orderId'],
            "status"=>1,
            "createtime"=>time()
        );
        $user = \app\common\model\User::where('id',$orderInfo['uid'])->find();
        if(empty($user)){
            return false;
        }
        $relation = implode(",",explode("-",$user['relation']));
        Db::query("UPDATE `pt_user` SET zhiNum=zhiNum+1 WHERE id in($relation)");
        $data["uidList"] = $relation;
        self::insert($data);
    }
    /**
     * 定时处理
     */
    public static function crontab()
    {
        $list = self::where('status',1)->order('id asc')->select();
        if(empty($list)){
            return true;
        }
        $agentList = \app\admin\model\agent\Agent::where('status',1)->order('id asc')
            ->field('id,title,price,thumb,zhi,ping,plevel,agent')
            ->select();
        foreach ($list as $ks=>$vs){
            if(!empty($vs['relation'])){
                $userList = Db::query("SELECT * FROM `pt_user` WHERE id in(".$vs['relation'].")");
                foreach ($userList as $kUser=>$vUser){
                    \app\admin\model\agent\Agent::crontabUp($vUser,$agentList);
                }
            }
            $vs->status = 2;
            $vs->updatetime = time();
            $vs->save();
        }
        return true;
    }







}
